#include "stdafx.h"
#include "DateTime.h"

#include <time.h>
#include <stdio.h>

namespace StdTools
{
	DateTime::DateTime(int seconds)
	{
		this->seconds = seconds;
	}


	DateTime::~DateTime(void)
	{
	}

	DateTime * DateTime::CurrentTime()
	{
		return new DateTime(time(NULL));
	}

	int DateTime::getTimestamp()
	{
		return this->seconds;
	}

	int DateTime::getHour()
	{
		return localtime(&this->seconds)->tm_hour;
	}

	int DateTime::getMinute()
	{
		return localtime(&this->seconds)->tm_min;
	}

	int DateTime::getSecond()
	{
		return localtime(&this->seconds)->tm_sec;
	}

	int DateTime::getDay()
	{
		return localtime(&this->seconds)->tm_mday;
	}

	int DateTime::getMonth()
	{
		return localtime(&this->seconds)->tm_mon;
	}

	int DateTime::getYear()
	{
		return localtime(&this->seconds)->tm_year+1900;
	}

	std::ostream& operator<< (std::ostream& os,const DateTime& t)
	{
		char buffer[5];
		struct tm * date = localtime(&t.seconds);
		sprintf(buffer,"%02d",date->tm_mday);
		os<<buffer<<"/";
		sprintf(buffer,"%02d",date->tm_mon+1);
		os<<buffer<<"/";
		os<<date->tm_year+1900<<" ";
		sprintf(buffer,"%02d",date->tm_hour);
		os<<buffer<<":";
		sprintf(buffer,"%02d",date->tm_min);
		os<<buffer<<":";
		sprintf(buffer,"%02d",date->tm_sec);
		os<<buffer;
		return os;
	}
}